﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using GtsServer.Common;
using System.Data;

namespace GtsServer.DAL {
    public partial class JournalDAL {
        public JournalDAL() {
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList() {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,UserId,UpStream,DownStream,RequestTime");
            strSql.Append(" FROM Journal ");
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(GtsServer.Model.Journal model) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Journal(");
            strSql.Append("UserId,UpStream,DownStream,RequestTime)");
            strSql.Append(" values (");
            strSql.Append("@UserId,@UpStream,@DownStream,@RequestTime)");
            SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@UpStream", SqlDbType.VarChar,2000),
                    new SqlParameter("@DownStream", SqlDbType.VarChar,2000),
                    new SqlParameter("@RequestTime", SqlDbType.DateTime)};
            parameters[0].Value = model.UserId;
            parameters[1].Value = model.UpStream;
            parameters[2].Value = model.DownStream;
            parameters[3].Value = model.RequestTime;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if(obj == null) {
                return 0;
            } else {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(GtsServer.Model.Journal model) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Journal set ");
            strSql.Append("UserId=@UserId,");
            strSql.Append("UpStream=@UpStream,");
            strSql.Append("DownStream=@DownStream,");
            strSql.Append("RequestTime=@RequestTime");
            strSql.Append(" where Id=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4),
                    new SqlParameter("@UpStream",SqlDbType.VarChar,2000),
                    new SqlParameter("@DownStream",SqlDbType.VarChar,2000),
                    new SqlParameter("@RequestTime",SqlDbType.DateTime),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.UserId;
            parameters[1].Value = model.UpStream;
            parameters[2].Value = model.DownStream;
            parameters[3].Value = model.RequestTime;
            parameters[4].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if(rows > 0) {
                return true;
            } else {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID) {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Journal ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if(rows > 0) {
                return true;
            } else {
                return false;
            }
        }

    }
}
